

package com.hazelcast.spi.impl.proxyservice.impl;

import com.hazelcast.core.DistributedObject;
import com.hazelcast.core.DistributedObjectEvent;
import com.hazelcast.core.DistributedObjectListener;
import com.hazelcast.core.DistributedObjectUtil;
import com.hazelcast.internal.util.executor.StripedRunnable;

import java.util.Collection;
import java.util.UUID;

final class ProxyEventProcessor implements StripedRunnable {

    private final Collection<DistributedObjectListener> listeners;
    private final DistributedObjectEvent.EventType type;
    private final String serviceName;
    private final String objectName;
    private final DistributedObject object;
    private final UUID source;

    ProxyEventProcessor(Collection<DistributedObjectListener> listeners, DistributedObjectEvent.EventType eventType, String serviceName, String objectName, DistributedObject object, UUID source) {
        this.listeners = listeners;
        this.type = eventType;
        this.serviceName = serviceName;
        this.objectName = objectName;
        this.object = object;
        this.source = source;
    }

    @Override
    public void run() {
        DistributedObjectEvent event = new DistributedObjectEvent(type, serviceName, objectName, object, source);
        for (DistributedObjectListener listener : listeners) {
            switch (type) {
            case CREATED:
                listener.distributedObjectCreated(event);
                break;
            case DESTROYED:
                listener.distributedObjectDestroyed(event);
                break;
            default:
                throw new IllegalStateException("Unrecognized EventType:" + type);
            }
        }
    }

    @Override
    public int getKey() {
        return DistributedObjectUtil.getName(object).hashCode();
    }
}
